package com.af.robot.controller;

import com.af.robot.config.ConfigThermalData;
import com.af.robot.result.ResultEnum;
import com.af.robot.scanner.Catalogue;
import com.af.robot.util.imp.NginxManager;
import com.af.robot.util.imp.TomcatManager;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Base64;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/catalogue")
public class CatalogueController {

    @Autowired
    private Catalogue catalogue;

    @Autowired
    private ConfigThermalData configThermalData;

    @Autowired
    private TomcatManager tomcatManager;

    @Autowired
    private NginxManager nginxManager;

    @PostMapping("/show/{type}/{name}")
    public JSONObject getDirInner(@PathVariable String type,@PathVariable String name,@RequestBody JSONObject data){
        JSONArray services = configThermalData.getDirConfig().getJSONArray(type);
        for (int i = 0; i < services.size(); i++) {
            JSONObject service = services.getJSONObject(i);
            if (name.equals(service.getString("af_name"))){
                return catalogue.getLikeNameType(service.getString("af_path") + new String(Base64.getDecoder().decode(data.getString("uri"))) ,data.getString("type"),data.getString("name"),service.getString("af_path"));
            }
        }
        return ResultEnum.SERVICE_NAME_ERROR.getResult();
    }

    @PostMapping("/down/{type}/{name}")
    public void fileDown(@PathVariable String type, @PathVariable String name, @RequestBody JSONObject data, HttpServletResponse response) throws IOException {
        JSONArray services = configThermalData.getDirConfig().getJSONArray(type);
        for (int i = 0; i < services.size(); i++) {
            JSONObject service = services.getJSONObject(i);
            if (name.equals(service.getString("af_name"))){
                BufferedInputStream fileStream = catalogue.getFileStream(service.getString("af_path") + new String(Base64.getDecoder().decode(data.getString("uri"))));
//                response.reset();
                if (fileStream == null){
                    response.setContentType("application/json");
                    response.getOutputStream().print(ResultEnum.SERVICE_NAME_ERROR.getResult().toJSONString());
                    return;
                }else {
                    File down = new File(service.getString("af_path") + new String(Base64.getDecoder().decode(data.getString("uri"))));
                    response.addHeader("Content-Length", down.length()+"");
                    response.setContentType("application/octet-stream");
                    response.addHeader("Content-Disposition", "attachment;filename="+down.getName());
                    byte[] buffer = new byte[fileStream.available()];
                    fileStream.read(buffer);
                    fileStream.close();
                    OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                    toClient.write(buffer);
                    toClient.flush();
                    toClient.close();
                    return;
                }
            }
        }
        response.setContentType("application/json");
        response.getOutputStream().print(ResultEnum.SERVICE_NAME_ERROR.getResult().toJSONString());
    }


}
